﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using DBConnection;
using System.Data.Entity;
using MySqlConnection;
using Framework;
namespace Data
{
    public static class ImageData
    {
        /// <summary>
        /// Get VOImage by image ID
        /// </summary>
        /// <param name="imageId">Image ID to get</param>
        /// <returns>Object VOImage</returns>
        public static VOImage GetImage(int imageId)
        {
            VOImage imgObj;
            using (DBHomeContainer dd = ContextDAO.DataContext)
            {
                ImageSet imgById = (from oImage
                            in dd.ImageSet
                           where oImage.Id == imageId
                           select oImage).FirstOrDefault();
                imgObj = VOMapperImage.ImageInstanceFromData(imgById);
            }
            return imgObj;
        }

        public static VOImage GetImage2(string id)
        {
            DBContainer dbCont = MySqlContext.DataContext;
            ImageClass img = dbCont.GetImageById(id);
            return VOMapperImage.ImageInstanceFromData(img);
        }

        public static List<VOImage> GetAllImages2()
        {
            List<VOImage> imgs = new List<VOImage>();
            DBContainer dbCont = MySqlContext.DataContext;
            List<ImageClass> allImages = dbCont.GetAllImages();
            foreach (ImageClass img in allImages)
            {
                imgs.Add(VOMapperImage.ImageInstanceFromData(img));
            }
            return imgs;
        }

        //public static IList<VOImage> GetAllImages()
        //{
            //List<VOImage> allImages = new List<VOImage>();
            //using (DBHomeContainer dd = ContextDAO.DataContext)
            //{
            //    foreach (ImageSet img in dd.ImageSet)
            //    {
            //        VOImage newImg = new VOImage(img.Id, img.Title, img.Description, img.Link, img.Data);
            //        allImages.Add(newImg);
            //    }
            //}
            //return allImages;
        //}

        public static void InsertImage(ImageClass img)
        {
            DBContainer dbCont = MySqlContext.DataContext;
            dbCont.CreateImage(img.name, img.description, DateTime.Now.ToString(), img.ext, img.data, img.size);
        }

        /// <summary>
        /// Create new image record 
        /// </summary>
        /// <param name="newImage">Object with all image properties</param>
        public static void CreateImage(VOImage newImage)
        {
            try
            {
                ImageSet img = ImageSet.CreateImage(1, newImage.Title, newImage.Description, newImage.Link, newImage.Data);
                if (img != null)
                {
                    img.Add();
                }
            }
            catch (Exception ex)
            {
            }
        }

        public static void DeleteImage(int imageId)
        {
            using (DBHomeContainer dd = ContextDAO.DataContext)
            {
                ImageSet imgById = (from oImage
                            in dd.ImageSet
                            where oImage.Id == imageId
                            select oImage).FirstOrDefault();
                imgById.Delete();
            }
        }

        public static void UpdateImage(VOImage imgObj)
        {
            DBContainer dbCont = MySqlContext.DataContext;
            dbCont.UpdateImage(MappingTool.CatchNull(imgObj.Id), imgObj.Title, imgObj.Description);
        }
    }
}